{% extends "base/base.html" %}

{% block title %}按职务统计 - 项目合作归属数据管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <h2><i class="bi bi-briefcase"></i> 按职务统计</h2>
        <p class="text-muted">根据职务统计各类人员的工作量和商谈情况</p>
    </div>
</div>

<!-- 搜索表单 -->
<div class="search-form">
    <form method="GET" class="row g-3">
        <div class="col-md-4">
            <label class="form-label">职务 <span class="text-danger">*</span></label>
            <select class="form-select" name="position" required>
                <option value="">请选择职务</option>
                {% for pos in positions %}
                <option value="{{ pos }}" {% if position == pos %}selected{% endif %}>{{ pos }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-md-3">
            <label class="form-label">开始时间 <span class="text-danger">*</span></label>
            <input type="date" class="form-control" name="start_date" value="{{ start_date }}" required>
        </div>
        <div class="col-md-3">
            <label class="form-label">结束时间 <span class="text-danger">*</span></label>
            <input type="date" class="form-control" name="end_date" value="{{ end_date }}" required>
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-search"></i> 查询
            </button>
        </div>
    </form>
</div>

<!-- 统计选项 -->
<div class="card mb-4">
    <div class="card-header">
        <h5 class="mb-0">统计选项</h5>
    </div>
    <div class="card-body">
        <form method="GET" id="optionsForm">
            <input type="hidden" name="position" value="{{ position }}">
            <input type="hidden" name="start_date" value="{{ start_date }}">
            <input type="hidden" name="end_date" value="{{ end_date }}">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="show_online" id="showOnline" {% if show_online %}checked{% endif %} onchange="this.form.submit()">
                        <label class="form-check-label" for="showOnline">
                            线上商谈统计
                        </label>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="show_offline" id="showOffline" {% if show_offline %}checked{% endif %} onchange="this.form.submit()">
                        <label class="form-check-label" for="showOffline">
                            线下商谈统计
                        </label>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 统计结果表格 -->
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">统计结果 - {{ position }}</h5>
        <button class="btn btn-outline-primary btn-sm" onclick="exportStats()">
            <i class="bi bi-download"></i> 导出统计
        </button>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>项目负责人</th>
                        <th>职务</th>
                        <th>具体职务</th>
                        <th>所属项目</th>
                        <th>线上商谈次数</th>
                        <th>线上商谈总时长</th>
                        <th>线上平均时长</th>
                        <th>线下商谈次数</th>
                        <th>线下商谈总时长</th>
                        <th>线下平均时长</th>
                        <th>累计总时长</th>
                        <th>累计总次数</th>
                        <th>平均时长</th>
                    </tr>
                </thead>
                <tbody>
                    {% for result in results %}
                    <tr>
                        <td>{{ result.staff_name }}</td>
                        <td>{{ result.position }}</td>
                        <td>{{ result.specific_position or '-' }}</td>
                        <td>{{ result.project_name or '-' }}</td>
                        <td>{{ result.online_count }}次</td>
                        <td>{{ "%.1f"|format(result.online_total_duration / 60) }}小时</td>
                        <td>{{ "%.1f"|format(result.online_avg_duration / 60) }}小时</td>
                        <td>{{ result.offline_count }}次</td>
                        <td>{{ "%.1f"|format(result.offline_total_duration / 60) }}小时</td>
                        <td>{{ "%.1f"|format(result.offline_avg_duration / 60) }}小时</td>
                        <td>{{ "%.1f"|format(result.total_duration / 60) }}小时</td>
                        <td>{{ result.total_count }}次</td>
                        <td>{{ "%.1f"|format(result.total_avg_duration / 60) }}小时</td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="13" class="text-center text-muted py-4">
                            {% if position and start_date and end_date %}
                                该职务在指定时间段内暂无商谈记录
                            {% else %}
                                请选择查询条件后点击查询按钮
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

{% if results %}
<!-- 汇总统计 -->
<div class="row mt-4">
    <div class="col-lg-3 col-md-6 mb-3">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4>{{ results|length }}</h4>
                        <p class="mb-0">该职务人员总数</p>
                    </div>
                    <div class="align-self-center">
                        <i class="bi bi-people-fill" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-3 col-md-6 mb-3">
        <div class="card bg-info text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4>{{ (results|sum(attribute='online_count')) or 0 }}</h4>
                        <p class="mb-0">线上商谈总次数</p>
                    </div>
                    <div class="align-self-center">
                        <i class="bi bi-laptop" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-3 col-md-6 mb-3">
        <div class="card bg-warning text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4>{{ (results|sum(attribute='offline_count')) or 0 }}</h4>
                        <p class="mb-0">线下商谈总次数</p>
                    </div>
                    <div class="align-self-center">
                        <i class="bi bi-building" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-3 col-md-6 mb-3">
        <div class="card bg-success text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4>{{ "%.1f"|format((results|sum(attribute='total_duration')) / 60) }}</h4>
                        <p class="mb-0">总商谈时长(小时)</p>
                    </div>
                    <div class="align-self-center">
                        <i class="bi bi-clock" style="font-size: 2rem;"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endif %}
{% endblock %}

{% block extra_js %}
<script>
function exportStats() {
    // 导出统计数据
    const params = new URLSearchParams(window.location.search);
    const exportUrl = '{{ url_for("statistics.export_stats_by_position") }}?' + params.toString();
    window.location.href = exportUrl;
}
</script>
{% endblock %}
